package com.bosssoft.ecds.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.AbstractWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.bosssoft.ecds.entity.dto.SentBillDto;
import com.bosssoft.ecds.util.EntityUtil;
import com.bosssoft.ecds.dao.FbeStockFinanceapplyCheckMapper;
import com.bosssoft.ecds.entity.dto.QueryDTO;
import com.bosssoft.ecds.entity.po.FbeStockFinanceapplyCheckPO;
import com.bosssoft.ecds.entity.po.FbeStockFinanceapplyItemPO;
import com.bosssoft.ecds.entity.vo.FbeStockFinanceapplyCheckVO;
import com.bosssoft.ecds.entity.vo.FbeStockFinanceapplyItemVO;
import com.bosssoft.ecds.service.AbstractService;
import com.bosssoft.ecds.service.FbeStockFinanceapplyCheckService;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import javax.annotation.Resource;
import java.math.BigInteger;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @author 张东海
 * @date 2020/8/18
 * @description
 */
@Service
public class FbeStockFinanceapplyCheckServiceImpl extends AbstractService<FbeStockFinanceapplyCheckPO, FbeStockFinanceapplyCheckMapper>
    implements FbeStockFinanceapplyCheckService {

    @Resource
    RabbitTemplate rabbitTemplate;

    public void submit(FbeStockFinanceapplyCheckPO financeapplyCheckPO) {
        rabbitTemplate.convertAndSend("amq.direct","fbKey",financeapplyCheckPO);
    }

    public void outBills(List<SentBillDto> sentBillDtos){
        for (SentBillDto sentBillDto : sentBillDtos) {
            rabbitTemplate.convertAndSend("amq.direct","billKey",sentBillDto);
        }

    }

    public List<FbeStockFinanceapplyCheckVO> getAllDetail(QueryDTO query) {
        AbstractWrapper wrapper = new QueryWrapper<FbeStockFinanceapplyCheckPO>();
        wrapper.eq("f_status",query.getChangeState());
        if(!StringUtils.isEmpty(query.getNo())){
            wrapper.eq("f_no",query.getNo());
        }
        if(!StringUtils.isEmpty(query.getAuthor())){
            wrapper.eq("f_author",query.getAuthor());
        }

        if(query.getPreDate() != null && query.getLastDate() != null){
            wrapper.between("f_submit_date",query.getPreDate(),query.getLastDate());
        }
        wrapper.last("LIMIT "+(query.getPage()-1)*query.getLimit()+","+query.getLimit());
        List<FbeStockFinanceapplyCheckPO> poList = mapper.selectList(wrapper);
        return poList.stream()
                .map(po -> {
                    FbeStockFinanceapplyCheckVO vo = new FbeStockFinanceapplyCheckVO();
                    return EntityUtil.POToVO(po, vo);
                })
                .collect(Collectors.toList());
    }

    @Override
    public List<FbeStockFinanceapplyItemVO> getItems(BigInteger id){
        List<FbeStockFinanceapplyItemPO> pos = mapper.getItems(id);
        return pos.stream()
                .map(po -> {
                    FbeStockFinanceapplyItemVO vo = new FbeStockFinanceapplyItemVO();
                    BeanUtil.copyProperties(po,vo);
                    return vo;
                })
                .collect(Collectors.toList());
    }
}
